//
//  SFlieManage.m
//  LSCodeLibrary
//
//  Created by Samuel on 15/4/1.
//  Copyright (c) 2015年 Samuel. All rights reserved.
//

#import "SFlieManage.h"

@implementation SFlieManage

#pragma mark 单例创建
+ (instancetype)shareSFlieManage
{
    static SFlieManage *Singleton=nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Singleton=[[SFlieManage alloc] init];
    });
    return Singleton;
}

#pragma mark 获取Documents目录
-(NSString *)getDocuments;
{
    NSString* path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    return path;
}
#pragma mark 获取Documents目录（带创建）
/**
 *  获取Documents目录（带创建）
 *
 *  @param path 后缀目录：（test1／test2/test3）
 *
 *  @return 完整路径(带沙盒路径)
 */
- (NSString*)getDocumentsPathWithCreateAndPath:(NSString*)path;
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *pathArray = [path componentsSeparatedByString:@"/"];
    NSString *pathComMix = [self getDocuments];
    for (NSString *getOnePath in pathArray) {
        pathComMix = [pathComMix stringByAppendingPathComponent:getOnePath];
        if (![fileManager fileExistsAtPath:pathComMix]) {
            [fileManager createDirectoryAtPath:pathComMix withIntermediateDirectories:YES attributes:nil error:nil];
        }
    }
    return pathComMix;
}
#pragma mark 获取Documents目录
/**
 *  获取Documents目录
 *
 *  @param path 后缀目录：（test1／test2/test3）
 *
 *  @return 完整路径(带沙盒路径)
 */
- (NSString*)getDocumentsPathWithOutCreateAndPath:(NSString*)path;
{
    NSArray *pathArray = [path componentsSeparatedByString:@"/"];
    NSString *pathComMix = [self getDocuments];
    for (NSString *getOnePath in pathArray) {
        pathComMix = [pathComMix stringByAppendingPathComponent:getOnePath];
    }
    return pathComMix;
}
#pragma mark 写入数据
/**
 *  写入数据本地
 *
 *  @param fileName 文件名
 *  @param path     路径（可以是完整路径，或着后缀路径）
 *  @param file     文件
 *
 *  @return 完整的路径
 */
- (NSString*)writeFileWithFileName:(NSString*)fileName
                           andPath:(NSString*)path
                           andFile:(id)file;
{
    path = [path stringByReplacingOccurrencesOfString:[self getDocuments] withString:@""];
    NSString *realPath = [[[self getDocuments] stringByAppendingPathComponent:path] stringByAppendingPathComponent:fileName];
    /**
     *  创建文件夹
     */
    [self getDocumentsPathWithCreateAndPath:path];
    if ([[file class] isSubclassOfClass:[NSString class]]) {
        BOOL res;
        if (fileName) {
            res = [file writeToFile:realPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
        }
        return (res)?[path stringByAppendingPathComponent:fileName]:nil;
    }
    else
    {
        BOOL res;
        if (fileName) {
            res = [file writeToFile:realPath atomically:YES];
        }
        return (res)?[path stringByAppendingPathComponent:fileName]:nil;
    }

    return nil;
}
/**
 *  写入数据本地
 *
 *  @param path     完整的路径(包括文件名)
 *  @param file     文件
 *
 *  @return 文件名
 */
- (NSString*)writeFileWithFileAllPathIncludeName:(NSString*)path
                                         andFile:(id)file;
{
    if ([[file class] isSubclassOfClass:[NSString class]]) {
        BOOL res;
        if (path) {
            res = [file writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
        }
        return (res)?[path stringByDeletingLastPathComponent]:nil;
    }
    else
    {
        BOOL res;
        if (path) {
            res = [file writeToFile:path atomically:YES];
        }
        return (res)?[path stringByDeletingLastPathComponent]:nil;
    }
    
    return nil;
}
#pragma mark 读data
/**
 *  读取文件（Data）
 *
 *  @param fileName 文件名
 *  @param path     路径（可以是完整路径，或着后缀路径）
 *
 *  @return data
 */
- (NSData*)readFileWithFileName:(NSString*)fileName andPath:(NSString*)path
{
    path = [path stringByReplacingOccurrencesOfString:[self getDocuments] withString:@""];
    NSString *realPath = [[[self getDocuments] stringByAppendingPathComponent:path] stringByAppendingPathComponent:fileName];
    NSData *data = [NSData dataWithContentsOfFile:realPath];
    return data;
}
/**
 *  读取文件（Data）
 *
 *  @param path     完整的路径(包括文件名)
 *
 *  @return data
 */
- (NSData*)readFileWithFileAllPathIncludeName:(NSString*)path;
{
    path = [[self getDocuments]stringByAppendingPathComponent:path];
    NSData *data = [NSData dataWithContentsOfFile:path];
    return data;
}
#pragma mark 删除文件
/**
 *  删除文件
 *
 *  @param path     完整的路径(包括文件名)
 *
 *  @return bool
 */
-(BOOL)deleteFileWithFileAllPathIncludeName:(NSString*)path;
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL res=[fileManager removeItemAtPath:path error:nil];
    return res;
}
/**
 *  删除文件
 *
 *  @param fileName 文件名
 *  @param path     路径（可以是完整路径，或着后缀路径）
 *
 *  @return bool
 */
-(BOOL)deleteFileWithFileName:(NSString*)fileName andPath:(NSString*)path
{
    path = [path stringByReplacingOccurrencesOfString:[self getDocuments] withString:@""];
    NSString *realPath = [[[self getDocuments] stringByAppendingPathComponent:path] stringByAppendingPathComponent:fileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL res=[fileManager removeItemAtPath:realPath error:nil];
    return res;
}
//NSString *content=[[NSString alloc] initWithData:[NSData dataWithContentsOfFile:documentsPath] encoding:NSUTF8StringEncoding]
// NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:documentsPath];
//NSArray *array = [NSArray arrayWithContentsOfFile:documentsPath];


@end
